<?php use_javascripts_for_form($form) ?>
<?php use_stylesheet('forms.css') //load css pf order page   ?>
<?php use_stylesheet('hoverTables.css') //load css of tables  ?>
<div id="form">  
    <!-- Back button -->
    <a href="<?php echo url_for('purchase/index') ?>" class="bt_back"><img src="<?php echo image_path('Buttons/bt_arrow_back.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_arrow_back_hov.png') ?>'" onmouseout="this.src='<?php echo image_path('Buttons/bt_arrow_back.png') ?>'"></a>
    <?php
    //Session functions
    
    //Retrieve the hidden variables and put them in an array
    //Then put the array in the session
    function setVariables($id){
        $array['id'] = $id;
        $array['number'] = $_POST['number']; 
        $array['amount'] = $_POST['amount'];
        $array['name'] = $_POST['name'];
        $array['unit'] = $_POST['unit'];
        $array['type'] = $_POST['type'];
        $array['amount'] = $_POST['amount'];
        $array['supplier'] = $_POST['supplier'];
        $array['price'] = $_POST['price'];
        $array['updated'] = $_POST['updated'];

        $i = 0;
        while(isset($_SESSION['clearinglist'][$i])){                  
            $i++;
        } 
        $_SESSION['clearinglist'][$i] = $array;
        unset($_POST['add']);
    } 
    
    if(isset($_POST['empty_purchase'])){
        if(isset($_SESSION['clearinglist'])){
            unset($_SESSION['clearinglist']);
        }
    }
    if(isset($_POST['removep'])){
        $idline = $_POST['removep'];
        unset($_SESSION['clearinglist'][$idline]);
    }            
    if(isset($_POST['addp'])) {
        $id = $_POST['id'];
         //check if part already exists in session
         //only add part when its not in session yet
        if(isset ($_SESSION['clearinglist'])){
            $inlist = false;
            foreach($_SESSION['clearinglist'] as $value){                 
                if($id == $value['id']){
                    $inlist = true;
                    break;
                }
            }
            if($inlist == false){
                setVariables($id);
            }
        } else{
            setVariables($id);
        }
        unset($_POST['add']);
    }
    if(isset($_POST['update'])){
        $i = 0;
        while(isset($_POST[''.$i.''])){
            $_SESSION['clearinglist'][$i]['amount'] = $_POST['amount'.$i.''];
            $i++;
        }    
        unset($_POST['update']);
    }
    ?> 
    <!-- The actual form -->
    <form action="<?php echo url_for('purchase/bookPurchases') ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
        <?php if (!$form->getObject()->isNew()): ?>
            <input type="hidden" name="sf_method" value="put" />
        <?php endif; ?>
        <fieldset>
            <legend class="bold">Purchases</legend>
            <table class="table_top">
                <tr class="bold head_small">
                    <td width="70px">Purchase No.</td>
                    <td width="70px">Material No.</td>
                    <td width="">Name</td>
                    <td width="">Type</td>
                    <td width="55px">Quantity</td>
                    <td>Unit</td>
                    <td width="">Supplier</td>
                    <td width="">Price</td>
                    <td width="120px">Last updated</td>
                    <td width="30px"></td>
                </tr>
            </table>
            <div id="scroll_div" class="table_div">
                <table>
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="visibility:hidden;">
                       <input type="hidden" value="Add" name="retp"/>
                    </form>
                    <tr class="bold head_small" height="15px">
                        <td width="70px"></td>
                        <td width="70px"></td>
                        <td width=""></td>
                        <td width=""></td>
                        <td width="55px"></td>
                        <td></td>
                        <td width=""></td>
                        <td width=""></td>
                        <td width="120px"></td>
                        <td width="20px"></td>
                    </tr>                    
                    <?php processList($purchases, $form); ?>
                </table>
           </div>
        </fieldset>
        <?php if(!empty($_SESSION['clearinglist'])){ //Check if the session has been set ?>
            <fieldset>
            <legend class="bold">Clearing List</legend>
            <div class="table_div">
                <table id="purchaselist">
                    <tr class="bold head_small">
                        <td>Purchase No.</td>
                        <td>Material No.</td>
                        <td>Name</td>
                        <td>Type</td>
                        <td>Quantity</td>
                        <td>Unit</td>
                        <td>Supplier</td>
                        <td>Price</td>
                        <td>Last Updated</td>
                        <td width="20px"></td>
                    </tr>
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                        <input type="submit" name="empty_purchase" value="" class="bt_empty" title="Empty entire clearinglist"/>
                    </form>
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                        <?php
                            foreach($_SESSION['clearinglist'] as $value){ ?> 
                                <?php $purchase = PurchaseTable::getInstance()->findOneBy('id', $value['id']); 
                                if($purchase->getStatus() != 'received'){ ?>
                                    <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> "><?php
                                    echo '<td>'.$value['id'].'</td>';
                                    echo '<td>'.$value['number'].'</td>';
                                    echo '<td>'.$value['name'].'</td>';
                                    echo '<td>'.$value['type'].'</td>';
                                    echo '<td>'.$value['amount'].'</td>';
                                    echo '<td>'.$value['unit'].'</td>';
                                    echo '<td>'.$value['supplier'].'</td>';
                                    echo '<td>'.$value['price'].'</td>';
                                    echo '<td>'.$value['updated'].'</td>';
                                    echo '<input type="hidden" name="id" value="'.$value['id'].'"/>';
                                    echo '<input type="hidden" name="name" value="'.$value['name'].'"/>';
                                    echo '<input type="hidden" name="type" value="'.$value['type'].'"/>';
                                    echo '<input type="hidden" name="number" value="'.$value['number'].'"/>';
                                    echo '<input type="hidden" name="amount" value="'.$value['amount'].'"/>';
                                    echo '<input type="hidden" name="unit" value="'.$value['unit'].'"/>';
                                    echo '<input type="hidden" name="supplier" value="'.$value['supplier'].'"/>';
                                    echo '<input type="hidden" name="price" value="'.$value['price'].'"/>';
                                    echo '<input type="hidden" name="'.array_search($value, $_SESSION['clearinglist']).'" value="'.array_search($value, $_SESSION['clearinglist']).'"/>';
                                    echo '<td><input type="submit" value="'.array_search($value, $_SESSION['clearinglist']).'" name="removep" class="bt_delete"/></td>';
                                    echo '</tr>';
                                }
                                else { 
                                    unset($_SESSION['clearinglist'][array_search($value, $_SESSION['clearinglist'])]);                           
                                }
                            }
                        ?>
                    </form>  
                </table>
            </div>
            </fieldset>
            <div class="hidden_csrf"><?php echo $form['_csrf_token']->renderRow(); ?></div>
            <input type="submit" value="" class="ok"/>
            <div style="margin: -35px 0 0 80px;">
                <!-- Print button -->
                <a href="<?php echo url_for('purchase_show_clear') ?>" target="_blank">
                    <img src="<?php echo image_path('Buttons/bt_print.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_print_hov.png') ?>';" onmouseout="this.src='<?php echo image_path('Buttons/bt_print.png') ?>';" title="Show Received List"/>
                </a>
            </div>
        <?php } ?>
    </form>    
</div> 

<!-- Retrieve all the data of each purchase and the data of the material belonging to that purchase -->
<?php function processList($list, $form){ ?>
     <?php foreach ($list as $i=>$purchase): ?>
        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <td><?php echo $purchase->getId() ?></td>
                <!-- Retrieve the material belonging to this purchase from the database -->
                <?php $material = PartTable::getInstance()->findOneBy('id', $purchase->getMaterialId()); ?>
                <td><?php echo $material->getPartNumber() ?></td>
                <td><?php echo $material->getName() ?></td>
                <td><?php echo $material->getType() ?></td>
                <td><?php echo $purchase->getAmount() ?></td>
                <td><?php echo $material->getUnit() ?></td>
                <!-- Check if a supplier has been assigned to this purchase
                     If so return the name of the supplier, if not return null -->
                <?php $supplierId = $purchase->getSupplierId(); 
                if($supplierId != null){
                    $supplier = ContactTable::getInstance()->findOneBy('id', $purchase->getSupplierId()); ?>
                    <td><?php echo $supplier->getName() ?></td>
                <?php } else { ?>
                    <td><?php echo null ?></td>
                <?php } ?>
                <td><?php echo $purchase->getPrice() ?></td>
                <td><?php echo $purchase->getUpdatedAt() ?></td>
                
                <!-- Put all the variables in hidden inputs so these that can be stored in the session -->
                <input type="hidden" name="id" value="<?php echo $purchase->getId() ?>"/>
                <input type="hidden" name="number" value="<?php echo $material->getPartNumber() ?>"/>
                <input type="hidden" name="name" value="<?php echo $material->getName() ?>"/>
                <input type="hidden" name="type" value="<?php echo $material->getType() ?>"/>
                <input type="hidden" name="unit" value="<?php echo $material->getUnit() ?>"/>     
                <input type="hidden" name="amount" value="<?php echo $purchase->getAmount() ?>"/>
                <input type="hidden" name="supplier" value="<?php if($supplierId != null){ echo $supplier->getName(); }
                                                                  else { echo null; } ?>"/>
                <input type="hidden" name="price" value="<?php echo $purchase->getPrice() ?>"/>
                <input type="hidden" name="updated" value="<?php echo $purchase->getUpdatedAt() ?>"/>
                <!-- The add purchase button -->
                <td><input type="submit" value="" name="addp" class="add_bt" title="Book purchase"/></td>
            </form>
        </tr>
<?php endforeach;
} ?>